<?php
namespace app\admin\service;

use think\Loader;
use app\admin\service\Common;
use app\admin\model\Company AS CompanyModel;
use app\admin\model\Stock;
use app\admin\service\Stock AS StockService;
use app\admin\service\ProfitForecast;
use app\admin\service\BalanceSheet;
use app\admin\service\FinanceCashFlow;
use app\admin\service\MainIndic;

class Company extends Common {

    const COMPANY_A = 'http://emweb.securities.eastmoney.com/PC_HSF10/CompanySurvey/CompanySurveyAjax?code=';
    const COMPANY_INFO_START = "https://datacenter.eastmoney.com/securities/api/data/v1/get?reportName=RPT_F10_BASIC_ORGINFO&columns=ALL&quoteColumns=&filter=(";
    const COMPANY_INFO_END = ")&pageNumber=1&pageSize=1&sortTypes=&sortColumns=&source=HSF10&client=PC&v=";
    // url.QueryEscape()
    protected $stock;
    public function _initialize(){
        $this->stock = new Stock();
    }

    public function Collect($where = [], $arr = []){
        $company = "";
        $company = file_get_contents(self::COMPANY_A.$where['jys'].$where['stock_code'].'&code='.$where['jys'].$where['stock_code']);
        if(!empty($company)){
            return self::CollectStock($where, json_decode($company, true));
        }
    }

    public static function CollectStock($where = [], $arr = []){
        $reCompanyInfo = "";
        $reCompanyInfo = file_get_contents(self::COMPANY_INFO_START.urlencode('SECUCODE="'.$where['stock_code'].'.'.$where['jys'].'"').self::COMPANY_INFO_END);
        if(!empty($reCompanyInfo)){
            $reCompanyInfo = json_decode($reCompanyInfo, true);
            $reCompanyInfo = $reCompanyInfo['result']['data'][0];
        }
        $arr = array_merge($arr, $reCompanyInfo);
        $stockId = 0;
        $stockId = StockService::Collect($where, $arr);
        if(!empty($stockId) && intval($stockId) > 0){

            self::CollectCompany($stockId,$where, $arr);
            BalanceSheet::Collect($stockId,$where);
            FinanceCashFlow::Collect($stockId,$where);
            FinanceProfit::Collect($stockId,$where);
            MainIndic::Collect($stockId,$where);
        }
        return 1;
    }

    public static function CollectCompany($stockId = 0, $stock = [], $arr = []){

        $reCompanyInfo = "";
        $reCompanyInfo = file_get_contents(self::COMPANY_INFO_START.urlencode('SECUCODE="'.$stock['stock_code'].'.'.$stock['jys'].'"').self::COMPANY_INFO_END);
        if(!empty($reCompanyInfo)){
            $reCompanyInfo = json_decode($reCompanyInfo, true);
        }

        $data = [];
        $data["name_used_before"] = '';
        if(!empty($arr['jbzl']['cym'])){
            $data["name_used_before"] = $arr['jbzl']['cym'];
        }
        $data["relevance_stock"] = '';
        $data["company_name"] = '';
        if(!empty($arr['jbzl']['gsmc'])){
            $data["company_name"] = $arr['jbzl']['gsmc'];
        }
        $data["company_name_en"] = '';
        if(!empty($arr['jbzl']['ywmc'])){
            $data["company_name_en"] = $arr['jbzl']['ywmc'];
        }
        $data["industry"] = '';
        if(!empty($arr['jbzl']['sshy'])){
            $data["industry"] = $arr['jbzl']['sshy'];
        }
        $data["region"] = '';
        if(!empty($arr['jbzl']['qy'])){
            $data["region"] = $arr['jbzl']['qy'];
        }
        $data["address"] = '';
        if(!empty($arr['jbzl']['bgdz'])){
            $data["address"] = $arr['jbzl']['bgdz'];
        }
        $data["reg_address"] = '';
        if(!empty($arr['jbzl']['zcdz'])){
            $data["reg_address"] = $arr['jbzl']['zcdz'];
        }
        $data["busine"] = '';
        if(!empty($arr['jbzl']['gsjj'])){
            $data["busine"] = $arr['jbzl']['gsjj'];
        }
        $data["main_busine"] = '';
        if(!empty($arr['jbzl']['jyfw'])){
            $data["main_busine"] = $arr['jbzl']['jyfw'];
        }
        $data["legal_person"] = '';
        if(!empty($arr['jbzl']['frdb'])){
            $data["legal_person"] = $arr['jbzl']['frdb'];
        }
        $data["chairman"] = '';
        if(!empty($arr['jbzl']['dsz'])){
            $data["chairman"] = $arr['jbzl']['dsz'];
        }
        $data["independent_director"] = '';
        if(!empty($arr['jbzl']['dlds'])){
            $data["independent_director"] = $arr['jbzl']['dlds'];
        }
        $data["actual_controller"] = '';
        if(!empty($arr['jbzl']['dlds'])){
            $data["actual_controller"] = $arr['ACTUAL_HOLDER'];
        }
        $data["create_time"] = '';
        if(!empty($arr['jbzl']['dlds'])){
            $data["create_time"] = $arr['FOUND_DATE'];
        }
        $data["come_market_time"] = '';
        if(!empty($arr['jbzl']['dlds'])){
            $data["come_market_time"] = $arr['LISTING_DATE'];
        }
        $data["manager"] = '';
        if(!empty($arr['jbzl']['zjl'])){
            $data["manager"] = $arr['jbzl']['zjl'];
        }
        $data["secretary_to_board"] = '';
        if(!empty($arr['jbzl']['dm'])){
            $data["secretary_to_board"] = $arr['jbzl']['dm'];
        }
        $data["phone"] = '';
        if(!empty($arr['jbzl']['lxdh'])){
            $data["phone"] = $arr['jbzl']['lxdh'];
        }
        $data["website"] = '';
        if(!empty($arr['jbzl']['gswz'])){
            $data["website"] = $arr['jbzl']['gswz'];
        }
        $data["email"] = '';
        if(!empty($arr['jbzl']['dzxx'])){
            $data["email"] = $arr['jbzl']['dzxx'];
        }
        $data["email"] = '';
        if(!empty($arr['jbzl']['dzxx'])){
            $data["email"] = $arr['jbzl']['dzxx'];
        }
        $data['business_rank'] = 0;
        $data['tradable_shares'] = 0;
        $data['total_tradable_shares'] = 0;
        $data['status'] = 0;

        $reVal = 0;
        if(CompanyModel::where(['sid'=>$stockId])->find()){
            $reVal = CompanyModel::where(['sid'=>$stockId])->update($data);
        }else{
            $data['sid'] = $stockId;
            $reVal = CompanyModel::insert($data);
        }
        return $reVal;
    }
}